﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Amazon.Services.S3.Samples.Properties;

namespace Amazon.Services.S3.Samples.Controls
{
    public partial class DirectoryInfoPanel : UserControl
    {
        #region Variables
        private Amazon.Services.S3.DirectoryInfo _directory;
        #endregion

        #region Properties
        public DirectoryInfo Directory
        {
            get { return this._directory; }
            set
            {
                this._directory = value;
                this.RefreshInformation();
            }
        }
        #endregion

        #region Constructors and Finalizers
        public DirectoryInfoPanel()
        {
            this.InitializeComponent();

            this.lstFiles.SmallImageList = new ImageList();
            this.lstFiles.SmallImageList.Images.Add(Resources.document_16);
            this.lstFiles.SmallImageList.Images.Add(Resources.folder_16);
        }
        #endregion

        #region Methods
        private void AddListItem(ListViewItem item)
        {
            this.lstFiles.Items.Add(item);
        }

        private void RefreshInformation()
        {
            if (this.Directory == null)
                return;

            this.lstFiles.Items.Clear();
            this.lblDrive.Text = this.Directory.Drive.Name;
            this.lblDirectory.Text = this.Directory.Name;

            ThreadPool.QueueUserWorkItem(new WaitCallback(this.RefreshFiles));
        }

        private void RefreshFiles(Object value)
        {
            DirectoryInfo[] directories = this.Directory.GetDirectories();
            for (int i = 0; i < directories.Length; i++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = 1;
                item.Text = directories[i].Name;
                item.SubItems.Add("Folder");

                this.lstFiles.BeginInvoke(new AddItemHandler(this.AddListItem), item);
            }

            FileInfo[] files = this.Directory.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                files[i].AccessControl = AccessControlModes.Public;
                ListViewItem item = new ListViewItem();
                item.ImageIndex = 0;
                item.Text = files[i].Name;
                item.SubItems.Add("Unknown");

                this.lstFiles.BeginInvoke(new AddItemHandler(this.AddListItem), item);
            }

        }
        #endregion

        #region Event Methods
        private void lstFiles_DragDrop(object sender, DragEventArgs e)
        {
            String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);

            for (int i = 0; i < files.Length; i++)
            {
                String filename = System.IO.Path.GetFileName(files[i]);
                Byte[] buffer = System.IO.File.ReadAllBytes(files[i]);
                File.WriteAllBytes(frmMain.Account, this.Directory.FullName.TrimEnd('\\') + "\\" + filename, buffer);
            }

            this.lstFiles.Items.Clear();
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.RefreshFiles));
        }

        private void lstFiles_DragEnter(object sender, DragEventArgs e)
        {
            if (this.Directory != null && e.Data.GetDataPresent(DataFormats.FileDrop, false))
                e.Effect = DragDropEffects.Copy;
        }
        #endregion

        #region Delegates
        private delegate void AddItemHandler(ListViewItem item);
        #endregion
    }
}
